//
//  DDSettingViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/3/27.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDSettingViewController.h"
#import "DDCommonCell.h"
#import "DDLoginManager.h"

@interface DDSettingViewController ()

@end

@implementation DDSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"设置";
    
    [self setUpView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setUpView
{
    self.tableView.tableFooterView = [self footerView];
}


- (NSMutableArray *)getSettingTitleArray
{
    NSMutableArray *titles = [[NSMutableArray alloc] init];
    [titles addObject:@[@{@"title": @"提醒设置"}]];
    [titles addObject:@[@{@"title": @"修改手机号"},@{@"title":@"修改密码"}]];
    [titles addObject:@[@{@"title": @"意见反馈"},@{@"title":@"我要评价"}, @{@"title":@"关于我们"}]];

    return titles;
}

- (UIView *)footerView
{
    UIView *footerLogoutView = [[UIView alloc] init];
    footerLogoutView.backgroundColor =COLOR_BACKGROUND;
    footerLogoutView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 60);
    UIButton *logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [logoutButton setTitle:@"退出登录" forState:UIControlStateNormal];
    [logoutButton setBackgroundColor:[UIColor whiteColor]];
    [logoutButton setTitleColor:COLOR_FONT_RED forState:UIControlStateNormal];
    logoutButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [logoutButton addTarget:self action:@selector(logoutClick) forControlEvents:UIControlEventTouchUpInside];
    
    [footerLogoutView addSubview:logoutButton];
    
    [logoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(20, 0, 0, 0));
        
    }];
    
    return footerLogoutView;
}

#pragma mark
#pragma mark - Private Method

- (void)logoutClick
{
    
    
    //退出登录Action
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要退出登录?" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 退出登录
        [[DDLoginManager sharedManager] loginOut];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertVC addAction:action1];
    [alertVC addAction:action2];
    [self.navigationController presentViewController:alertVC animated:true completion:nil];
    
//    //退出登录Action
//    UIAlertView *alert = [UIAlertView alertViewWithTitle:nil message:@"确定要退出登录？" cancelButtonTitle:@"取消" didDismissBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
//        if (buttonIndex == 0) {
//            // 退出登录
//            [[DDLoginManager sharedManager] loginOut];
//            
//           
//        } else {
//            // 取消
//            
//        }
//    } otherButtonTitles:@"确定", nil];
//    
//    [alert show];
}

#pragma mark - UITableView DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self getSettingTitleArray] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self getSettingTitleArray][section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CustomCellIdentifier = @"DDCommonCell";
    
    DDCommonCell *cell = (DDCommonCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DDCommonCell" owner:self options:nil] lastObject];
    }
    
    NSDictionary *dic = [self getSettingTitleArray][indexPath.section][indexPath.row];
    cell.titleLabel.text = dic[@"title"];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
